﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GDesktop.Service;
using GDesktop.Arquitetura;
using Google.Contacts;
using Google.GData.Client;
using GDesktop.Service.DTO;

namespace GDesktop.Contatos
{
    public partial class MainContatos : Form
    {
        public MainContatos()
        {
            InitializeComponent();
            dtgGrupos.AutoGenerateColumns = false;
            dtgContatos.AutoGenerateColumns = false;
            ContatosService = new ContatosService(Aplicacao.NomeAplicacao, Aplicacao.Login, Aplicacao.Password);
        }

        #region "Propriedades"
        public ContatosService ContatosService { get; set; }
        #endregion

        private void MainContatos_Load(object sender, EventArgs e)
        {
            this.SetupForm();
        }

        private void SetupForm()
        {
            this.FillGroupsGrid();
        }

        private void FillGroupsGrid()
        {
            dtgGrupos.DataSource = ContatosService.GetAllGroups();
        }

        #region "Eventos"
        private void dtgGrupos_SelectionChanged(object sender, EventArgs e)
        {
            DataGridView corrente = (DataGridView)sender;

            if (corrente.SelectedRows.Count > 0)
            {
                Group grupoSelecionado = (Group)corrente.SelectedRows[0].DataBoundItem;

                this.FillGridContacts(grupoSelecionado);

            }
        }

        private void FillGridContacts(Group grupoSelecionado)
        {
            this.dtgContatos.DataSource = this.ContatosService.GetContactsByGroup(grupoSelecionado.Id);
        }

        private void dtgContatos_SelectionChanged(object sender, EventArgs e)
        {
            DataGridView corrente = (DataGridView)sender;

            if (corrente.SelectedRows.Count > 0)
            {
                ContactsDTO contatoSelecionado = (ContactsDTO)corrente.SelectedRows[0].DataBoundItem;
                string id = contatoSelecionado.Contact.Id;
                EditarUsuario edicao = new EditarUsuario();
                edicao.Setup(id);
                pnlEdicao.Controls.Clear();
                pnlEdicao.Controls.Add(edicao);
            }
        }

        #endregion



    }
}
